package cn.com.dingtek.genius.manager.server.event;

import cn.com.dingtek.genius.manager.server.repository.db.entity.UserOperateLog;
import cn.com.dingtek.genius.manager.server.repository.db.manager.UserOperateLogManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Slf4j
@Component
public class UserOperateEventListener implements ApplicationListener<UserOperateEvent> {
    @Autowired
    private UserOperateLogManager userOperateLogManager;

    @Override
    public void onApplicationEvent(UserOperateEvent event) {
        log.info("{}", event);
        System.out.println("========" + Thread.currentThread().getName());

        try {
            userOperateLogManager.save(UserOperateLog.builder()
                    .userId(event.getUserId())
                    .accountName(event.getAccountName())
                    .operateDesc(event.getOperateDesc())
                    .operateModule(event.getOperateModule())
                    .operateIp(event.getOperateIp())
                    .logState(event.getLogState())
                    .ctime(new Date())
                    .build());
        } catch (Exception e) {
            log.error("user operate log error.event:{}", event, e);
        }

    }
}
